package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.demo.domain.SysMenu;
import com.demo.domain.SysPrivilege;
import com.demo.model.RolePrivilegesParam;
import com.demo.service.SysMenuService;
import com.demo.service.SysPrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.mapper.SysRolePrivilegeMapper;
import com.demo.domain.SysRolePrivilege;
import com.demo.service.SysRolePrivilegeService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

@Service
public class SysRolePrivilegeServiceImpl extends ServiceImpl<SysRolePrivilegeMapper, SysRolePrivilege> implements SysRolePrivilegeService{

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 新增权限服务
     */
    @Autowired
    private SysPrivilegeService sysPrivilegeService;

    /**
     * 涉及中间表 sys_role_privilege
     *
     */
    @Autowired
    private SysRolePrivilegeService sysRolePrivilegeService;
    @Override
    public List<SysMenu> findSysMenuAndPrivilege(Long roleId) {

        //查询所有菜单
        List<SysMenu> menus = sysMenuService.list();

        //页面显示的是二级菜单 和二级菜单下的权限
        if(CollectionUtils.isEmpty(menus)){
            return Collections.emptyList();
        }

        //一级菜单 没有父菜单的菜单
        List<SysMenu> rootMenus = menus.stream().filter(sysMenu -> sysMenu.getParentId() == null).collect(Collectors.toList());

        if(CollectionUtils.isEmpty(rootMenus)){
            return Collections.emptyList();
        }
        //subMenus :二级菜单集合
        List<SysMenu> subMenus = new ArrayList<>();
        for (SysMenu rootMenu : rootMenus){
            subMenus.addAll(getChildMenus(rootMenu.getId(),roleId,menus));
        }
        return subMenus;
    }



    /**
     * 查询菜单的子菜单 (递归)
     * @param parentId
     * @param roleId
     * @param sources
     * @return
     */
    private List<SysMenu> getChildMenus(Long parentId, Long roleId, List<SysMenu> sources) {
        List<SysMenu> chlids = new ArrayList<>();
        for (SysMenu  source : sources) {
            if (parentId.equals(source.getParentId() == null?"":source.getParentId())){
                chlids.add(source);
                source.setChilds(getChildMenus(source.getId(),roleId,sources));
                List<SysPrivilege> sysPrivileges = sysPrivilegeService.getAllSysPrivilege(source.getId(),roleId);
                source.setPrivileges(sysPrivileges);
            }
        }
        return chlids;
    }

    /**
     * 授予角色权限
     * @param rolePrivilegesParam
     * @return
     */
    @Override
    @Transactional
    public boolean grantPrivileges(RolePrivilegesParam rolePrivilegesParam) {
        Long roleId = rolePrivilegesParam.getRoleId();
        //先删除该角色之前的权限   LambdaQueryWrapper<SysRolePrivilege>().eq(SysRolePrivilege::getRoleId,roleId):查询sys_role_privilege表中 roleId为前端 roleID的数据
         sysRolePrivilegeService.remove(new LambdaQueryWrapper<SysRolePrivilege>().eq(SysRolePrivilege::getRoleId, roleId));
            List<Long> privilegeIds = rolePrivilegesParam.getPrivilegeIds();
            if(!CollectionUtils.isEmpty(privilegeIds)){
                List<SysRolePrivilege> sysRolePrivileges = new ArrayList<>();
                for (Long privilegeId : privilegeIds) {
                    SysRolePrivilege sysRolePrivilege = new SysRolePrivilege();
                    sysRolePrivilege.setRoleId(roleId);
                    sysRolePrivilege.setPrivilegeId(privilegeId);
                    sysRolePrivileges.add(sysRolePrivilege);
                }
                //新增现在角色全新的权限
                boolean b = sysRolePrivilegeService.saveBatch(sysRolePrivileges);
                return b;
            }
       return true;
    }
}
